package com.example.lotterysystem.service.activitystatus.operater;

import com.example.lotterysystem.dao.dataobject.ActivityPrizeDo;
import com.example.lotterysystem.dao.mapper.ActivityMapper;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.example.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator {

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        if(activityId == null
                || prizeId == null
                || targetPrizeStatus == null){
            return false;
        }
        ActivityPrizeDo activityPrizeDo = activityPrizeMapper.selectByAPId(activityId , prizeId);
        if(activityPrizeDo == null){
            return false;
        }

        if(targetPrizeStatus.name().equalsIgnoreCase(activityPrizeDo.getStatus())){
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityPrizeStatusEnum targetPrizeSatus = convertActivityStatusDTO.getTargetPrizeStatus();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        try{
            activityPrizeMapper.updateStatus(activityId , prizeId , targetPrizeSatus.name());
            return true;
        }catch (Exception e){
            return false;
        }

    }
}












